<?php
namespace app\index\controller;

use think\Controller;
use think\Request;


//微信支付
class Rechargewepay extends Controller{
	
	//发起微信支付
	public function index(){

		$param = [
			'body' =>Request::instance()->param('body'),
			'orderid' =>Request::instance()->param('orderid'),
			'pay_no' =>Request::instance()->param('pay_no'),
			'price' =>Request::instance()->param('price'),
			'notify_url' =>Request::instance()->param('notify_url'),
		];
		
		Vendor('weixinpay.do.JSAPI2');
		$hp = new \JSAPI2();
		
		list($jsApiParameters,$editAddress) = $hp->GetParam($param['body'],$param['pay_no'],$param['price'],$param['notify_url']);
		
		return view('',[
			'orderid' => $param['orderid'],
			'jsApiParameters'=>$jsApiParameters,
			'editAddress'=>$editAddress
		]);
	}
	
	//异步通知
	public function notify_url(){
		Vendor('weixinpay.do.NOTIFY_RECHARGE');
		$hp = new \NOTIFY2();
		$hp->Start();
	}
	
	//支付完毕，点完成按钮进入该页面
	public function return_url(){
		$orderid = Request::instance()->param('orderid');
		//$this->redirect('Order/pay',['orderid'=>$orderid]);
		/*
		/api/recharge/resQuery.tsl
		input:
		ordertype  //orderamount(货款) cashamount(现金) depositamount(保证金)
		orderid

		C20200705092840721BA9AA

		*/
		
		$head = strlen($orderid) > 0 ? substr($orderid,0,1) : '';
		
		$ordertype = '';
		if($head == 'C')
			$ordertype = 'cashamount';
		
		$pm = [
			'ordertype' => $ordertype,
			'orderid' => $orderid
		];
		$res = \ServerHelper::post('^/recharge/resQuery',$pm);
		$data = json_decode($res,JSON_UNESCAPED_UNICODE);
		
		if(!isset($data['flag']))
			return view('return_error');
		else
			return view('',$data);
		
	}
	

}
